import { ref } from "vue";
import { setItem, getItem } from "@/utils/LocalStorageHelper.js";

export function useToggleBookmark() {
  const showBookmark = ref(false);

  function toggleShowBookmark() {
    showBookmark.value = !showBookmark.value;
    console.log("showBookmark changed to:", showBookmark.value);
  }
  return {
    showBookmark,
    toggleShowBookmark,
  };
}

export function useBookmark() {
  // 确保初始化为数组，而不是null
  const bookmarkNews = ref(getItem("bookmark") || []);
  // 添加收藏
  function addBookmark(bookmarkNew) {
    //更新响应式变量的数据
    bookmarkNews.value = [...new Set([...bookmarkNews.value, bookmarkNew])];
    //更新 localStorage中的数据
    setItem("bookmark", bookmarkNews.value);
  }
  // 取消收藏
  function removeBookmark(bookmarkNew) {
    //更新响应式变量的数据
    bookmarkNews.value = bookmarkNews.value.filter(
      (item) => item.title !== bookmarkNew.title
    );
    // 更新 localStorage中的数据
    setItem("bookmark", bookmarkNews.value);
  }

  return {
    bookmarkNews,
    addBookmark,
    removeBookmark,
  };
}
